package com.zhuss.advice.processor;

import com.zhuss.advice.db.dao.ext.ProposalDao;
import com.zhuss.advice.db.dao.ext.ProposalSonDao;
import com.zhuss.advice.db.entity.Proposal;
import com.zhuss.advice.db.entity.ProposalSon;
import com.zhuss.bean.bo.advice.proposalSon.CreateProposalSonBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.ProposalStatus;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/6 14:53
 */
@Component("proposalSonCreate")
public class ProposalSonCreate extends AbstractProcessor<Object, CreateProposalSonBo> implements Processor<Object, CreateProposalSonBo> {

    @Autowired
    private ProposalSonDao proposalSonDao;

    @Autowired
    private ProposalDao proposalDao;

    @Override
    public void check(CreateProposalSonBo createProposalSonBo) throws WpsException {

    }

    @Override
    public Object process(CreateProposalSonBo bo) throws WpsException {
        ProposalSon son = new ProposalSon();
        son.setProposalId(bo.getProposalId());
        son.setSonContent(bo.getSonContent());
        son.setArea(bo.getConcurrentArea());

        son.setCreator(bo.getConcurrentUserName());
        son.setCreatorId(bo.getConcurrentUserId());
        son.setCreateTime(new Date());
        proposalSonDao.create(son);

        Proposal p = new Proposal();
        p.setProposalId(bo.getProposalId());
        p.setStatus(ProposalStatus.已回复.key());
        proposalDao.updateByPrimaryKeySelective(p);
        return null;
    }

    @Override
    public Object log(CreateProposalSonBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.意见建议.key());
        record.setAction(LogActionEnum.投诉创建.key());
        record.setTargetId(param.getTargetId());
        record.setTargetName(param.getSonContent());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
